package com.usian.controller;



import com.usian.message.Message;
import com.usian.room.Room;
import com.usian.roomuser.RoomUser;
import com.usian.service.RoomService;
import com.usian.user.User;
import com.usian.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/app/room")
public class RoomController {
    @Autowired
    private RoomService roomService;
    @Autowired
    private RedisTemplate redisTemplate;
    //敏感库
    private Set<String> sensitiveWords = new HashSet<>();


    @RequestMapping("/roomList")
    public List<Room> roomList(){
        List<Room> roomList = redisTemplate.opsForList().range("roomList", 0, -1);
        if (roomList ==null){
            roomList = roomService.roomList();
        }
        return roomList;
    }
    @RequestMapping("/addRoom")
    public Room addRoom(@RequestBody Room room){
        // 设置房间的日期为当前日期
        room.setRoomDate(new Date());
        // 调用服务层方法添加房间
        roomService.addRoom(room);
        // 获取房间列表
        List<Room> roomList = roomService.roomList();
        // 清空旧数据
        redisTemplate.delete("roomList");
        redisTemplate.delete("roomUser");
        // 存储新数据
        for (Room room1 : roomList) {
            // 获取房间所属用户的头像
            User user = roomService.getUserById(room1.getRoomUser());
            // 设置房间的头像为用户头像
            room1.setImg(user.getImg());

            // 将房间信息推入Redis列表中
            redisTemplate.opsForList().rightPush("roomList", room1);
        }
        // 根据房间用户ID和房间号获取房间信息
        Room roomUser = roomService.getRoomById(room.getRoomUser(),room.getRoomNumber());
        // 插入房间用户信息
        roomService.insertRoomUser(roomUser);
        // 创建消息对象
        Message message = new Message();
        // 设置消息发送者为房主
        message.setSendName("系统");
        // 设置消息内容
        message.setMessage("房间创建成功,本通知框用作系统通知,玩家交流等,请勿语言攻击他人,文明上网");
        // 将消息推入Redis列表中
        redisTemplate.opsForList().rightPush(roomUser.getId()+"message", message);
        // 设置键的过期时间为30分钟
        redisTemplate.expire(roomUser.getId() + "message", 30, TimeUnit.MINUTES);
        // 返回房间用户信息
        return roomUser;

    }
    //
    @RequestMapping("/roomUser")
    public List<RoomUser> roomUser(){
        List<RoomUser> user = roomService.getRoomUser();
        return user;
    }
    @RequestMapping("/delRoomUser/{roomid}/{userid}")
    public String delRoomUser(@PathVariable Integer roomid,@PathVariable Integer userid){
        roomService.delRoomUser(roomid,userid);
        return "success";
    }
    //接收消息
    @RequestMapping("/showMessage/{roomid}")
    public List<Message> showMessage(@PathVariable String roomid){
        List<Message> message = redisTemplate.opsForList().range(roomid+"message", 0, -1);
        redisTemplate.expire(roomid + "message", 30, TimeUnit.MINUTES);

        return message;
    }

    public RoomController() {
        // 初始化敏感词库，实际应用中可以从数据库或其他配置文件加载
        sensitiveWords.add("操你妈");
        sensitiveWords.add("傻逼");
        sensitiveWords.add("你妈死了");
        sensitiveWords.add("二逼");
        sensitiveWords.add("废物");
        sensitiveWords.add("草泥马");
        sensitiveWords.add("艹");
        sensitiveWords.add("妈的");
        sensitiveWords.add("尼玛");
        sensitiveWords.add("日你");
        sensitiveWords.add("去你的");
        sensitiveWords.add("干你");
        sensitiveWords.add("批");
        sensitiveWords.add("贱人");
        sensitiveWords.add("垃圾");
        sensitiveWords.add("畜生");
        sensitiveWords.add("人机");
        sensitiveWords.add("王八蛋");
        sensitiveWords.add("SB");
        sensitiveWords.add("你妈没了");
        sensitiveWords.add("我是你爹");
        sensitiveWords.add("我你爹");
        sensitiveWords.add("FUCK");
        sensitiveWords.add("Bitch");
        sensitiveWords.add("卧槽");
        sensitiveWords.add("草");
        sensitiveWords.add("傻帽");
        sensitiveWords.add("智障");
        sensitiveWords.add("神经病");
        sensitiveWords.add("变态");
        sensitiveWords.add("畜牲");
        sensitiveWords.add("混蛋");
        sensitiveWords.add("滚");
        sensitiveWords.add("杂种");
        sensitiveWords.add("狗");
        sensitiveWords.add("猪");
        sensitiveWords.add("王八");
        sensitiveWords.add("屌");
        sensitiveWords.add("婊子");
        sensitiveWords.add("贱货");
        sensitiveWords.add("低能");
        sensitiveWords.add("白痴");
        sensitiveWords.add("疯子");
        sensitiveWords.add("疯狗");
        sensitiveWords.add("傻逼玩意");
        sensitiveWords.add("傻逼东西");
        sensitiveWords.add("操");
        sensitiveWords.add("草拟吗");
        sensitiveWords.add("草泥玛");
        sensitiveWords.add("草泥码");
        sensitiveWords.add("草泥妈");
        sensitiveWords.add("草泥");
        sensitiveWords.add("煞笔");
        sensitiveWords.add("沙比");
        sensitiveWords.add("沙壁");
        // 添加更多敏感词...
    }

    // 敏感词过滤方法
    private String filterSensitiveWords(String content,Integer roomId) {
        for (String word : sensitiveWords) {
            if (content.contains(word)) {
                content = content.replaceAll(word, "*".repeat(word.length()));
                Message message = new Message();
                message.setSendName("系统");
                message.setMessage("请大家文明交流，避免使用敏感词或脏话。");
                redisTemplate.opsForList().rightPush(roomId+"message", message);
            }
        }
        return content;
    }
    //发送消息
    @RequestMapping("/sendMessage")
    public String sendMessage(@RequestBody Message message){
        // 使用RedisTemplate的opsForList方法来操作列表
        // 这里将消息对象推入到以message.getRoomId()+"message"为键的列表的右侧（尾部）
        // 参数1: key，列表的键，由消息的房间ID和"message"组成，用于区分不同的房间的消息
        // 参数2: value，要推入列表的消息对象

        if (message.getRoomId() != null) {
            // 过滤消息内容中的敏感词
            String filteredContent = filterSensitiveWords(message.getMessage(),message.getRoomId());
            message.setMessage(filteredContent);

            redisTemplate.opsForList().rightPush(message.getRoomId() + "message", message);
        }
        // 返回操作结果，此处简化为返回一个成功的信号
        // 实际应用中可能需要根据rightPush的返回值来确定操作是否成功
        return "success";
    }
    @RequestMapping("/delRoom/{roomid}")
    public String delRoom(@PathVariable Integer roomid){
        roomService.delRoom(roomid);
        return "success";
    }
    @RequestMapping("/getRoomUser/{roomid}")
    public List<User> getRoomUser(@PathVariable Integer roomid){
        List<User> roomUser = roomService.getRoomUsers(roomid);
        return roomUser;
    }
    @RequestMapping("/addUser")
    public String addUser(@RequestBody RoomUser roomUser){
        roomService.addUser(roomUser);
        return "success";
    }
    /**
     *
     * 接口：根据传过来的id来查看redis里面是否有对应的数据
     */
    @GetMapping("/checkGameStart/{id}")
    public Result getRoomUserId(@PathVariable Integer id){
        List<User> roomUser = redisTemplate.opsForList().range(id+"game", 0, -1);
//        判断是否为空
        if (roomUser != null && roomUser.size() == 0) {
            return null;
        }
        return Result.success();
    }

}
